import * as VueRouter from 'vue-router'
import Home from '@/views/Home.vue'
import Category from '@/views/Category.vue'
import ProductDetail from '@/views/ProductDetail.vue'
import UserCenter from '@/views/UserCenter.vue'
import Cart from '@/views/Cart.vue'
import Coupon from '@/views/Coupon.vue'
import OrderList from '@/views/OrderList.vue'
import Address from '@/views/Address.vue'
import Profile from '@/views/Profile.vue'
import Balance from '@/views/Balance.vue'
import Contact from '@/views/Contact.vue'
import Investment from '@/views/Investment.vue'
import Coop from '@/views/Coop.vue'
import Customize from '@/views/Customize.vue'
import OrderPayment from '@/views/OrderPayment.vue'
import Payment from '@/views/Payment.vue'
import OrderDetail from '@/views/OrderDetail.vue'
import AfterSales from '@/views/AfterSales.vue'
import OrderReview from '@/views/OrderReview.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import ForgotPassword from '@/views/ForgotPassword.vue'
import ChangePassword from '@/views/ChangePassword.vue'
import ChangePhone from '@/views/ChangePhone.vue'
import BindPhone from '@/views/BindPhone.vue'
import PaymentPassword from '@/views/PaymentPassword.vue'
import Recharge from '@/views/Recharge.vue'
import Withdraw from '@/views/Withdraw.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
  },
  {
    path: '/category',
    name: 'Category',
    component: Category,
  },
  {
    path: '/detail',
    name: 'ProductDetail',
    component: ProductDetail,
  },
  {
    path: '/user',
    name: 'UserCenter',
    component: UserCenter,
  },
  {
    path: '/cart',
    name: 'Cart',
    component: Cart,
  },
  {
    path: '/coupon',
    name: 'Coupon',
    component: Coupon,
  },
  {
    path: '/orders',
    name: 'OrderList',
    component: OrderList,
  },
  {
    path: '/address',
    name: 'Address',
    component: Address,
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
  },
  {
    path: '/balance',
    name: 'Balance',
    component: Balance,
  },
  {
    path: '/contact',
    name: 'contact',
    component: Contact,
  },
  {
    path: '/investment',
    name: 'investment',
    component: Investment,
  },
  {
    path: '/coop',
    name: 'coop',
    component: Coop,
  },
  {
    path: '/customize',
    name: 'customize',
    component: Customize,
  },
  {
    path: '/order-payment',
    name: 'OrderPayment',
    component: OrderPayment,
  },
  {
    path: '/payment',
    name: 'Payment',
    component: Payment,
  },
  {
    path: '/order-detail/:id',
    name: 'OrderDetail',
    component: OrderDetail,
  },
  {
    path: '/after-sales/:id',
    name: 'AfterSales',
    component: AfterSales,
  },
  {
    path: '/order-review/:id',
    name: 'OrderReview',
    component: OrderReview,
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: ForgotPassword,
  },
  {
    path: '/change-password',
    name: 'ChangePassword',
    component: ChangePassword,
  },
  {
    path: '/change-phone',
    name: 'ChangePhone',
    component: ChangePhone,
  },
  {
    path: '/bind-phone',
    name: 'BindPhone',
    component: BindPhone,
  },
  {
    path: '/payment-password',
    name: 'PaymentPassword',
    component: PaymentPassword,
  },
  {
    path: '/recharge',
    name: 'Recharge',
    component: Recharge,
  },
  {
    path: '/withdraw',
    name: 'Withdraw',
    component: Withdraw,
  },
]

const router = VueRouter.createRouter({
  history: VueRouter.createWebHashHistory(),
  routes,
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 需要登录的页面列表
  const requiresAuth = [
    '/user',
    '/profile',
    '/orders',
    '/address',
    '/balance',
    '/coupon',
    '/cart',
    '/order-payment',
    '/payment',
    '/order-detail',
    '/after-sales',
    '/order-review',
    '/change-password',
    '/change-phone',
    '/bind-phone',
    '/payment-password',
    '/recharge',
    '/withdraw',
  ]

  // 检查当前路由是否需要登录
  const needsAuth = requiresAuth.some((path) => {
    if (path.includes(':')) {
      // 处理动态路由，如 /order-detail/:id
      const pathPattern = path.replace(/:\w+/g, '\\w+')
      const regex = new RegExp(`^${pathPattern}$`)
      return regex.test(to.path)
    }
    return to.path === path
  })

  if (needsAuth) {
    // 检查是否已登录
    const userInfo = localStorage.getItem('userInfo')
    let isLoggedIn = false
    if (userInfo) {
      const parsedUserInfo = JSON.parse(userInfo) || {}
      isLoggedIn = parsedUserInfo.id && parsedUserInfo.id !== '' && parsedUserInfo.access_token && parsedUserInfo.access_token !== ''
    }
    if (!isLoggedIn) {
      // 未登录，重定向到登录页面，并保存原始路径
      next({
        path: '/login',
        query: { redirect: to.fullPath },
      })
      return
    }
  }

  next()
})

export default router
